<?php
/**
 * Context
 * provides information about the current application context, such as
 * the controller, action names and request
 *
 * @package    Lazy Framework
 * @subpackage controller
 * @author     Eduardo Marcolino <eduardo.marcolino@gmail.com>
 * @copyright  (c) 2010 Eduardo Marcolino <eduardo.marcolino@gmail.com>
 */

class Context
{
	protected
	$request           = null,
	$controller_name   = null,
	$action_name       = null,
	$controller        = null,
	$user              = null,
	$twig              = null;

	protected static
	$instance          = null;


	protected function initialize()
	{
		$this->request = new WebRequest($_REQUEST,$_FILES);
		
		$this->loadTemplateEngine();
		 
		$entities = new PPAOutletORMImpl(MODELS_DIR);
		if ($entities->hasEntities())
		{
			$databases = Spyc::YAMLLoad(CONFIG_DIR.'/databases.yaml');
			$outlet_config = array_merge($databases['connections'][APP_ENVIRONMENT],$entities->getClasses());
			Outlet::init($outlet_config);
			$outlet = Outlet::getInstance();
			$outlet->createProxies();
		}

		$routes = Spyc::YAMLLoad(CONFIG_DIR.'/routes.yaml');
		$m = Net_URL_Mapper::getInstance();

		foreach ( $routes['routes'] as $r) { $m->connect(WEB_PATH.$r['path'], $r['defaults']); }

		$route = $m->match($_SERVER['REQUEST_URI']);

		if (is_null($route)) { throw new PageNotFoundException("No Router Found for " . $_SERVER['REQUEST_URI']); }

		$controller_file = CONTROLLERS_DIR.'/'.$route['controller'].'.php';
		if( !file_exists($controller_file) ) { throw new ControllerNotFoundException('Controller '.$route['controller'].' not found'); }

		require_once($controller_file);

		$this->controller_name = $route['controller'];
		$this->action_name = $route['action'];

		$this->controller = new $route['controller'];

	}

	/**
	 * Retrieve the singleton instance of this class.
	 *
	 * @return Context A Context implementation instance.
	 */
	public static function getInstance()
	{
		if (!isset(self::$instance))
		{
			$class = __CLASS__;
			self::$instance = new $class();
			
			try {
				self::$instance->initialize();
			} catch (PageNotFoundException $ex) {
				$template = self::$instance->twig->loadTemplate('error404.html');
				echo $template->render(array('error'=>$ex->getMessage()));
				exit;
			}
		}

		return self::$instance;
	}

	protected function loadTemplateEngine()
	{
		Twig_Autoloader::register();

		$loader = new Twig_Loader_Filesystem(
		array(VIEWS_DIR,LAZY_FRAMEWORK_DATA_DIR.'/views'));

		$this->twig = new Twig_Environment($loader, array(
  		'cache' => CACHE_DIR,
  		'debug' => APP_ENVIRONMENT == 'dev' ? true : false,
		)); 
	}
	
	public function getTemplate()
	{
		return $this->twig;
	}

	/**
	 * @return Outlet
	 */
	public function getOutletInstance()
	{
		return Outlet::getInstance();
	}


	public function getActionName()
	{
		return $this->action_name;
	}

	/**
	 * @return Controller
	 */
	public function getController()
	{
		return $this->controller;
	}

	public function getControllerName()
	{
		return $this->controller_name;
	}

	/**
	 * @return WebRequest
	 */
	public function getRequest()
	{
		return $this->request;
	}

	public function getUser()
	{
		return $this->user;
	}

}